using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MKDS_Course_Editor
{
    public partial class nlcr_viewer : UserControl
    {
        public event MouseEventHandler ColorClicked;
        List<Color> colors_ = new List<Color>();
        bool bpp8_ = false;
        public void addColor()
        {
            List<Color> clo = new List<Color>();
            for (int i = 0; i < this.Controls.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p = (ColorPanel)this.Controls[i];
                clo.Add(p.PanelColor);
            }
            Colors.Clear();
            clo.Add(Color.Black);
            Colors.AddRange(clo.ToArray());
            repaint();
        }
        public void removeColor()
        {
            List<Color> clo = new List<Color>();
            for (int i = 0; i < this.Controls.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p = (ColorPanel)this.Controls[i];
                clo.Add(p.PanelColor);
            }
            Colors.Clear();
            clo.RemoveAt(clo.Count-1);
            Colors.AddRange(clo.ToArray());
            repaint();
        }
        public List<Color> Colors
        {
            get { return colors_; }
            set { colors_ = value; repaint(); }
    }
        public bool bpp8
        {
            get { return bpp8_; }
            set { bpp8_ = value; repaint(); }
        }

        public nlcr_viewer()
        {
            InitializeComponent();
        }
        public void repaint()
        {
            this.Controls.Clear();
            int row = 0;
            int coulom = 0;
            for (int i = 0; i < colors_.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p.BackColor = colors_[i];
                p.Size = new Size(16, 16);
                if (bpp8_)
                {
                    if ((coulom * 21) + 5 > this.Width - 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                else
                {
                    if (coulom == 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                p.PanelColor = colors_[i];
                p.Location = new Point(5 + (coulom * 21), 5 + (row * 21));
                p.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                p.AllowDrop = true;
                p.MouseEnter += new EventHandler(p_MouseEnter);
                p.MouseLeave += new EventHandler(p_MouseLeave);
                p.DragOver += new DragEventHandler(p_DragOver);
                p.MouseDown += new MouseEventHandler(p_MouseDown);
                p.DragDrop += new DragEventHandler(p_DragDrop);
                p.DragLeave += new EventHandler(p_DragLeave);
                p.MouseUp += ColorClicked;
                p.MouseMove += new MouseEventHandler(p_MouseMove);
                this.Controls.Add(p);
                coulom++;
            }
        }
        private void nlcr_viewer_Load(object sender, EventArgs e)
        {
            int row = 0;
            int coulom = 0;
            for (int i = 0; i < colors_.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p.BackColor = colors_[i];
                p.Size = new Size(16, 16);
                if (bpp8_)
                {
                    if ((coulom * 21) + 5 > this.Width - 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                else
                {
                    if (coulom == 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                p.PanelColor = colors_[i];
                p.Location = new Point(5 + (coulom * 21), 5 + (row * 21));
                p.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                p.AllowDrop = true;
                p.MouseEnter += new EventHandler(p_MouseEnter);
                p.MouseLeave += new EventHandler(p_MouseLeave);
                p.DragOver += new DragEventHandler(p_DragOver);
                p.MouseDown += new MouseEventHandler(p_MouseDown);
                p.DragDrop += new DragEventHandler(p_DragDrop);
                p.DragLeave += new EventHandler(p_DragLeave);
                p.MouseUp += ColorClicked;
                p.MouseMove += new MouseEventHandler(p_MouseMove);
                this.Controls.Add(p);
                coulom++;
            }
        }

        private void nlcr_viewer_Paint(object sender, PaintEventArgs e)
        {
            
        }

        void p_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                ColorPanel p = new ColorPanel();
                p = (ColorPanel)sender;
                p.DoDragDrop(p, DragDropEffects.Move);
            }
        }
        Color d;
        Color f;
        ColorPanel c;
        void p_DragLeave(object sender, EventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            try
            {
                d = c.PanelColor;
                f = p.PanelColor;
                p.BackColor = f;
                c.BackColor = d;
            }
            catch { };
            
        }

        void p_DragDrop(object sender, DragEventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            e.Effect = DragDropEffects.Move;
            c = (ColorPanel)e.Data.GetData(p.GetType());
            d = c.PanelColor;
            f = p.PanelColor;
            p.BackColor = d;
            p.PanelColor = d;
            c.BackColor = f;
            c.PanelColor = f;
        }

        void p_MouseDown(object sender, MouseEventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            ColorClicked(sender, e);
            //p.DoDragDrop(p, DragDropEffects.Move);
        }

        void p_DragOver(object sender, DragEventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            e.Effect = DragDropEffects.Move;
            c = (ColorPanel)e.Data.GetData(p.GetType());
            d = c.PanelColor;
            f = p.PanelColor;
            p.BackColor = d;
            c.BackColor = f;
        }

        void p_MouseEnter(object sender, EventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            p.BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

        void p_MouseLeave(object sender, EventArgs e)
        {
            ColorPanel p = new ColorPanel();
            p = (ColorPanel)sender;
            p.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            
        }

        public class ColorPanel : Panel
        {
            public Color PanelColor = Color.Black;
        }

        private void nlcr_viewer_Resize(object sender, EventArgs e)
        {
            this.Controls.Clear();
            int row = 0;
            int coulom = 0;
            for (int i = 0; i < colors_.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p.BackColor = colors_[i];
                p.Size = new Size(16, 16);
                if (bpp8_)
                {
                    if ((coulom * 21) + 5 > this.Width - 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                else
                {
                    if (coulom == 16)
                    {
                        row++;
                        coulom = 0;
                    }
                }
                p.PanelColor = colors_[i];
                p.Location = new Point(5 + (coulom * 21), 5 + (row * 21));
                p.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                p.AllowDrop = true;
                p.MouseEnter += new EventHandler(p_MouseEnter);
                p.MouseLeave += new EventHandler(p_MouseLeave);
                p.DragOver += new DragEventHandler(p_DragOver);
                p.MouseDown += new MouseEventHandler(p_MouseDown);
                p.DragDrop += new DragEventHandler(p_DragDrop);
                p.DragLeave += new EventHandler(p_DragLeave);
                p.MouseUp += ColorClicked;
                p.MouseMove += new MouseEventHandler(p_MouseMove);
                this.Controls.Add(p);
                coulom++;
            }
        }
        

        private void nlcr_viewer_SizeChanged(object sender, EventArgs e)
        {
            
        }
        public Color[] getPallete()
        {
            List<Color> clo = new List<Color>();
            for (int i = 0; i < this.Controls.Count; i++)
            {
                ColorPanel p = new ColorPanel();
                p = (ColorPanel)this.Controls[i];
                clo.Add(p.PanelColor);
            }
            return clo.ToArray();
        }
    }
}
